package com.example.abe.diandi.trends.fragment;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.BindView;
import com.example.abe.diandi.R;
import com.example.abe.diandi.common.TouchScaleImageView;
import com.example.abe.diandi.trends.model.ImageTextModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import example.abe.com.framework.imageloader.ImageLoader;
import example.abe.com.framework.imageloader.handle.BitmapHandle;
import example.abe.com.framework.viewinject.ViewInjectUtils;

public class ImageBrowseDialogFragment extends DialogFragment {

    private static final String IMAGE_LIST = "image_list";
    private static final String CURRENT_ID = "current_id";

    static ImageBrowseDialogFragment newInstance(ArrayList<ImageTextModel> listImage, String id) {
        ImageBrowseDialogFragment f = new ImageBrowseDialogFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(IMAGE_LIST, listImage);
        args.putString(CURRENT_ID, id);
        f.setArguments(args);
        return f;
    }

    @BindView(R.id.frag_image_browse_dialog_vp)
    protected ViewPager mVp;

    private MyAdapter mAdapter;

    private List<ImageTextModel> mListImgTextModel;

    private String mCurId;

    private HashMap<Integer, TouchScaleImageView> mMapIv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_image_browse_dialog, container, false);
        ViewInjectUtils.inject(this, v);
        initData();
        initView();
        return v;
    }

    private void initData() {
        mListImgTextModel = getArguments().getParcelableArrayList(IMAGE_LIST);
        mCurId = getArguments().getString(CURRENT_ID);
        mMapIv = new HashMap<>();
    }

    private void initView() {
        mAdapter = new MyAdapter();
        mVp.setAdapter(mAdapter);

        for (int i = 0; i<mListImgTextModel.size(); i++){
            if (mListImgTextModel.get(i).getId().equals(mCurId)){
                mVp.setCurrentItem(i);
                break;
            }
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogFullscreen);
        return dialog;
    }

    public class MyAdapter extends PagerAdapter {
        public MyAdapter() {
            super();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = mMapIv.get(position);
            mMapIv.remove(position);
            if (view != null){
                container.removeView(view);
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final TouchScaleImageView view = new TouchScaleImageView(getContext(), mVp);
            ViewGroup.LayoutParams lp = new ViewPager.LayoutParams();
            view.setLayoutParams(lp);
            view.setSingleClickListener(onSingleClickListener);
            view.setInterceptListener(onInterceptTouchListener);
            ImageLoader.getInstance().getBitmap(mListImgTextModel.get(position).getImage(),
                    new BitmapHandle.BitmapHandleListener() {
                        @Override
                        public void onHandle(Bitmap bitmap) {
                            view.setImageBitmap(bitmap);
                            view.requestLayout();
                        }
                    });
            container.addView(view);
            mMapIv.put(position, view);
            return view;
        }

        @Override
        public int getCount() {
            return mListImgTextModel.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

    private TouchScaleImageView.OnSingleClickListener onSingleClickListener =  new TouchScaleImageView.OnSingleClickListener() {
        @Override
        public boolean onSingleClick() {
            ImageBrowseDialogFragment.this.dismiss();
            return false;
        }
    };

    private TouchScaleImageView.OnInterceptTouchListener onInterceptTouchListener = new TouchScaleImageView.OnInterceptTouchListener() {
        @Override
        public boolean OnInterceptTouch(boolean isIntercept, MotionEvent ev) {
            if (!isIntercept){
                //support-core-ui-25.00中的ViewPager中，使用内部拦截法取消拦截时，内容会闪动一下。
                //查看源码，需要手动调用下面方法，使配合事件传递生命周期
                ev.setAction(MotionEvent.ACTION_DOWN);
                mVp.onInterceptTouchEvent(ev);
                mVp.onTouchEvent(ev);
            }
            return false;
        }
    };
}